package com.meishij.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jash.myutils.utils.ViewUtils;
import com.meishij.R;
import com.meishij.entry.FindCaiDanEntry;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 阿飞 on 2016/10/13.
 */

public class FindCaiDanRecyclerAdapter extends RecyclerView.Adapter<FindCaiDanRecyclerAdapter.ViewHolder> {

    private Context context;
    private List<FindCaiDanEntry.Data> list;

    public FindCaiDanRecyclerAdapter(Context context, List<FindCaiDanEntry.Data> list) {
        this.context = context;
        this.list = list;
        if (list == null) {
            this.list = new ArrayList<>();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflateView = LayoutInflater.from(context).inflate(R.layout.activity_find_detail_caidan_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflateView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.initView(holder.rootView);
        FindCaiDanEntry.Data data = list.get(position);
        ViewUtils.setTextView(holder.tv_find_detail_caidan_title, data.getTitle());
        ViewUtils.setTextView(holder.tv_find_detail_caidan_recipeNum, data.getRecipe_num() + "篇菜谱");
        List<String> photos = data.getPhotos();
        switch (photos.size()) {
            case 4:
                String img4 = photos.get(3);
                if (!TextUtils.isEmpty(img4)) {
                    Uri img4Uri = Uri.parse(img4);
                    holder.iv_find_detail_caidan_item_image4.setImageURI(img4Uri);
                }
            case 3:
                String img3 = photos.get(2);
                if (!TextUtils.isEmpty(img3)) {
                    Uri img3Uri = Uri.parse(img3);
                    holder.iv_find_detail_caidan_item_image3.setImageURI(img3Uri);
                }
            case 2:
                String img2 = photos.get(1);
                if (!TextUtils.isEmpty(img2)) {
                    Uri img2Uri = Uri.parse(img2);
                    holder.iv_find_detail_caidan_item_image2.setImageURI(img2Uri);
                }
            case 1:
                String img1 = photos.get(0);
                if (!TextUtils.isEmpty(img1)) {
                    Uri img1Uri = Uri.parse(img1);
                    holder.iv_find_detail_caidan_item_image1.setImageURI(img1Uri);
                }

        }

    }

    public void add(FindCaiDanEntry.Data item) {
        list.add(item);
        notifyDataSetChanged();
    }

    public void addAll(List<FindCaiDanEntry.Data> items) {
        list.addAll(items);
        notifyDataSetChanged();
    }

    public void clear() {
        list.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View rootView;

        TextView tv_find_detail_caidan_title;
        TextView tv_find_detail_caidan_recipeNum;
        SimpleDraweeView iv_find_detail_caidan_item_image1;
        SimpleDraweeView iv_find_detail_caidan_item_image2;
        SimpleDraweeView iv_find_detail_caidan_item_image3;
        SimpleDraweeView iv_find_detail_caidan_item_image4;

        public ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
        }

        public void initView(View rootView) {
            tv_find_detail_caidan_title = (TextView) rootView.findViewById(R.id.tv_find_detail_caidan_title);
            tv_find_detail_caidan_recipeNum = (TextView) rootView.findViewById(R.id.tv_find_detail_caidan_recipeNum);
            iv_find_detail_caidan_item_image1 = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_detail_caidan_item_image1);
            iv_find_detail_caidan_item_image2 = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_detail_caidan_item_image2);
            iv_find_detail_caidan_item_image3 = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_detail_caidan_item_image3);
            iv_find_detail_caidan_item_image4 = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_detail_caidan_item_image4);
        }
    }

}
